<?php
class loginModel extends model{
	public $userid;
	public $base;
	function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="user";
		$this->userid=$this->getUserId();
	}
	
	public function set($k,$v){
		$_SESSION[$k]=$v;
	}
	
	public function get($k){
		return $_SESSION[$k];
	}
	
	public function getUser($userid=0){
		$userid=$userid?$userid:(isset($_SESSION['ssuser']['userid'])?intval($_SESSION['ssuser']['userid']):0);
		if(!$userid) return false;
		$user=parent::selectRow(array("where"=>"userid=$userid"));
		unset($user['salt']);
		unset($user['password']);
		unset($user['paypwd']);
		return $user;
	}
	
	public function getUserId(){
		return (isset($_SESSION['ssuser']['userid'])?intval($_SESSION['ssuser']['userid']):0);
	}
	
	public function checklogin($ajax=0){
		if(empty($_SESSION['ssuser']['userid'])){
			C()->goAll("请先登录",1000,0,"/index.php?m=login&a=login");
		}
		
	}
	
	public function getAdmin($id=0){
		$id=$id?$id:intval($_SESSION['ssadmin']['id']);
		if(!$id) return false;
		return parent::setTable('admin')->selectRow(array("where"=>"id=$id"));
	}
	
	public function checkAdminLogin($ajax=0){
		if(get_post('ajax')) $ajax=1;
		if($ajax){
			if(empty($_SESSION['ssadmin'])){
				exit(json_encode(array("error"=>1,"message"=>C()->lang['please_login'])));
			}
		}else{
			if(empty($_SESSION['ssadmin'])){
				C()->gomsg(C()->lang['please_login'],APPADMIN."?m=admin_login");
			}
		}
	}
	
	public function getShopAdmin($adminid=0){
		$adminid=$adminid?$adminid:intval($_SESSION['ssshopadmin']['adminid']);
		if(!$adminid) return false;
		return parent::setTable('shopadmin')->selectRow(array("where"=>" adminid=$adminid "));
	}
	
	public function getShop($shopid=0){
		if(!$shopid){
			$shopid=intval($_SESSION['ssshopadmin']['shopid']);
		}
		if(!$shopid) return false;
		$data= parent::setTable('shop')->selectRow(array("where"=>"shopid=".$shopid));
		if(empty($data)) return false;
		if($data['userid']==0) $data['userid']=1;
		return $data;
	}
	
	public function checkShopAdmin(){
	
	}
	/*夜猫店*/
	public function getYmdianAdmin($adminid=0){
		$adminid=$adminid?$adminid:intval($_SESSION['ssymdianadmin']['adminid']);
		if(!$adminid) return false;
		return parent::setTable('ymdian_admin')->selectRow(array("where"=>" adminid=$adminid "));
	}
	
	public function getYmdian($shopid=0){
		if(!$shopid){
			$shopid=intval($_SESSION['ssymdianadmin']['shopid']);
		}
		if(!$shopid) return false;
		$data= parent::setTable('ymdian')->selectRow(array("where"=>"id=".$shopid));
		if(empty($data)) return false;
		 
		return $data;
	}
	/*End夜猫店*/
	/*口袋店*/
	public function getkoudaiAdmin($adminid=0){
		$adminid=$adminid?$adminid:intval($_SESSION['sskoudaiadmin']['adminid']);
		if(!$adminid) return false;
		return parent::setTable('koudai_admin')->selectRow(array("where"=>" adminid=$adminid "));
	}
	
	public function getkoudai($shopid=0){
		if(!$shopid){
			$shopid=intval($_SESSION['sskoudaiadmin']['shopid']);
		}
		if(!$shopid) return false;
		$data= parent::setTable('koudai')->selectRow(array("where"=>"id=".$shopid));
		if(empty($data)) return false;
 
		return $data;
	}
	/*End 口袋店*/
	/*快递员*/
	public function CheckKdyuan(){
		if(empty($_SESSION['sskdyuan'])){
			C()->goall("请先登录",1,"",APPINDEX."?m=kdyuan&a=login");
		}
	}
	
	public function kdyuanLogout(){
		$this->set('sskdyuan',false);
	}
	/*End 快递员*/
	/**
	 * array(
	 * 	"userid"=>$userid,
	 *  "passowrd"=>$password
	 * )
	 * **/
	public function setCode($user){
		$pwd=substr($user['password'],0,6);
		$agent=substr($_SERVER['HTTP_USER_AGENT'],0,6);
		$data=array(
			"u"=>$user['userid'],
			"p"=>$pwd,
			"a"=>$agent
		);
		$authcode=jiami(json_encode($data));
		return $authcode;
	}
	public function getCode($code){
		$data=json_decode(jiemi($code),true);
		if(empty($data)){
			return false;
		}
		$agent=substr($_SERVER['HTTP_USER_AGENT'],0,6);
		if( $agent!=$data['a']){
			return false;
		}
		return $data;
		
	} 
	public function CodeLogin(){
		if(get_post('authcode')){
			$authcode=get_post('authcode');			
		}else{
			$authcode=$_COOKIE['authcode'];
		}
		 
		if($authcode=='' or $authcode=='null') return false;
		
		$arr=$this->getCode($authcode); 
		 
		$userid=intval($arr['u']);
		$key="login_codelogin_".$userid;
		$user=M('user')->selectRow(array("where"=>"userid='".$userid."' "));	
				
		if($c=cache()->get($key)){
			if($authcode==$c){
				$this->userid=$user['userid'];
				$this->set("ssuser",$user);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($user) or $arr['p']!=substr($user['password'],0,6)){			
				setcookie("authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=$this->setCode($user);
				$this->userid=$user['userid']; 
				$this->set("ssuser",$user);
				cache()->set($key,$authcode,3600);
				setcookie("authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	/*库存管理自动登录*/
	public function kc_CodeLogin(){
		if(get_post('kc_authcode')){
			$authcode=get_post('kc_authcode');			
		}else{
			$authcode=$_COOKIE['kc_authcode'];
		}
		if($authcode=='' or $authcode=='null') return false;
		$authcode=jiemi($authcode);
		$arr=explode("|",$authcode);
		$adminid=intval($arr[0]);
		$key="login_kc_codelogin_".$adminid;
		$admin=M('kucun_admin')->selectRow(array("where"=>"adminid='".$adminid."' "));		
		if($c=cache()->get($key)){
			if($authcode==jiemi($c)){
				$this->set("sskucunadmin",$admin);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($admin) or $arr[1]!=umd5($admin['password'])){			
				setcookie("kc_authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=jiami($admin['adminid']."|".umd5($admin['password']));
				$this->set("sskucunadmin",$admin);
				cache()->set($key,$authcode,3600);
				setcookie("kc_authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	
	
	/*夜猫店管理自动登录*/
	public function ym_CodeLogin(){
		if(get_post('ym_authcode')){
			$authcode=get_post('ym_authcode');			
		}else{
			$authcode=$_COOKIE['ym_authcode'];
		}
		if($authcode=='' or $authcode=='null') return false;
		$authcode=jiemi($authcode);
		$arr=explode("|",$authcode);
		$adminid=intval($arr[0]);
		$key="login_ym_codelogin_".$adminid;
		$admin=M('ymdian_admin')->selectRow(array("where"=>"adminid='".$adminid."' "));		
		if($c=cache()->get($key)){
			if($authcode==jiemi($c)){
				$this->set("ssymdianadmin",$admin);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($admin) or $arr[1]!=umd5($admin['password'])){			
				setcookie("ym_authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=jiami($admin['adminid']."|".umd5($admin['password']));
				$this->set("ssymdianadmin",$admin);
				cache()->set($key,$authcode,3600);
				setcookie("ym_authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	/*口袋店管理自动登录*/
	public function kd_CodeLogin(){
		if(get_post('kd_authcode')){
			$authcode=get_post('kd_authcode');			
		}else{
			$authcode=$_COOKIE['kd_authcode'];
		}
		if($authcode=='' or $authcode=='null') return false;
		$authcode=jiemi($authcode);
		$arr=explode("|",$authcode);
		$adminid=intval($arr[0]);
		$key="login_kd_codelogin_".$adminid;
		$admin=M('koudai_admin')->selectRow(array("where"=>"adminid='".$adminid."' "));		
		if($c=cache()->get($key)){
			if($authcode==jiemi($c)){
				$this->set("sskoudaiadmin",$admin);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($admin) or $arr[1]!=umd5($admin['password'])){			
				setcookie("kd_authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=jiami($admin['adminid']."|".umd5($admin['password']));
				$this->set("sskoudaiadmin",$admin);
				cache()->set($key,$authcode,3600);
				setcookie("kd_authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	
	/*创业街店管理自动登录*/
	public function shop_CodeLogin(){
		if(get_post('shop_authcode')){
			$authcode=get_post('shop_authcode');			
		}else{
			$authcode=$_COOKIE['shop_authcode'];
		}
		if($authcode=='' or $authcode=='null') return false;
		$authcode=jiemi($authcode);
		$arr=explode("|",$authcode);
		$adminid=intval($arr[0]);
		$key="login_shop_codelogin_".$adminid;
		$admin=M('shopadmin')->selectRow(array("where"=>"adminid='".$adminid."' "));		
		if($c=cache()->get($key)){
			if($authcode==jiemi($c)){
				$this->set("ssshopadmin",$admin);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($admin) or $arr[1]!=umd5($admin['password'])){			
				setcookie("shop_authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=jiami($admin['adminid']."|".umd5($admin['password']));
				$this->set("ssshopadmin",$admin);
				cache()->set($key,$authcode,3600);
				setcookie("shop_authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	
	
	public function tg_CodeLogin(){
		if(get_post('tg_authcode')){
			$authcode=get_post('tg_authcode');			
		}else{
			$authcode=$_COOKIE['tg_authcode'];
		}
		if($authcode=='' or $authcode=='null') return false;
		$authcode=jiemi($authcode);
		$arr=explode("|",$authcode);
		$adminid=intval($arr[0]);
		$key="login_tg_codelogin_".$adminid;
		$admin=M('tgyuan_admin')->selectRow(array("where"=>"adminid='".$adminid."' "));		
		if($c=cache()->get($key)){
			if($authcode==jiemi($c)){
				$this->set("sstgyuanadmin",$admin);
			}else{
				cache()->set($key,"");
			}
		}else{
			
			if(empty($admin) or $arr[1]!=umd5($admin['password'])){			
				setcookie("tg_authcode","",time()-3999,"/",DOMAIN);		
			}else{
				$authcode=jiami($admin['adminid']."|".umd5($admin['password']));
				$this->set("sstgyuanadmin",$admin);
				cache()->set($key,$authcode,3600);
				setcookie("tg_authcode",$authcode,time()+3600000,"/",DOMAIN);
				
			}		
		}
	}
	
	public function getaccess(){
		$isvip=$islogin=0;
		if($_SESSION['ssuser']) {
			$islogin=1;
			$vip=parent::setTable("user_vip")->selectRow("userid=".$_SESSION['ssuser']['userid']);
			if($vip && $vip['endtime']>time() ){
				$isvip=1;
			}
			$isvip=1;
		}
		if($_SESSION['ssshopadmin'] or $_SESSION['ssadmin']){
			$isadmin=1;
			$isvip=1;
			$islogin=1;
		}
		
		$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